﻿
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text

''' <summary>
''' Exposes all the EntitySets in a EntityDataSource as a list of PropertyDescriptor objects.
''' </summary>
''' <remarks>
''' This is required for showing the available EntitySets in the drop down that
''' appears when editing the "DataMember" property of a complex bound control.
''' 
''' Reflected from DataViewManagerListItemTypeDescriptor
''' </remarks>
Public Class EntitySetTypeDescriptor
    Implements ICustomTypeDescriptor

#Region "Fields"

    Private m_DataSource As EntityDataSource
    Private m_DescriptorCollection As PropertyDescriptorCollection

#End Region

#Region "Methods"

    Friend Sub New(ByVal dataSource As EntityDataSource)
        m_DataSource = dataSource
    End Sub

    Friend Sub Reset()
        m_DescriptorCollection = Nothing
    End Sub

#End Region

#Region "ICustomTypeDescriptor"

    Private Function GetProperties(ByVal attributes As Attribute()) As PropertyDescriptorCollection Implements ICustomTypeDescriptor.GetProperties
        If m_DescriptorCollection Is Nothing Then
            Dim properties As PropertyDescriptor() = Nothing
            Dim count As Integer = m_DataSource.EntitySets.Count
            properties = New PropertyDescriptor(count - 1) {}
            For i As Integer = 0 To count - 1
                properties(i) = New EntitySetPropertyDescriptor(m_DataSource.EntitySets(i))
            Next
            m_DescriptorCollection = New PropertyDescriptorCollection(properties)
        End If
        Return m_DescriptorCollection
    End Function

    Private Function GetPropertyOwner(ByVal pd As PropertyDescriptor) As Object Implements ICustomTypeDescriptor.GetPropertyOwner
        Return Me
    End Function

    Private Function GetAttributes() As AttributeCollection Implements ICustomTypeDescriptor.GetAttributes
        Return New AttributeCollection(Nothing)
    End Function

    Private Function GetClassName() As String Implements ICustomTypeDescriptor.GetClassName
        Return Nothing
    End Function

    Private Function GetComponentName() As String Implements ICustomTypeDescriptor.GetComponentName
        Return Nothing
    End Function

    Private Function GetConverter() As TypeConverter Implements ICustomTypeDescriptor.GetConverter
        Return Nothing
    End Function

    Private Function GetDefaultEvent() As EventDescriptor Implements ICustomTypeDescriptor.GetDefaultEvent
        Return Nothing
    End Function

    Private Function GetDefaultProperty() As PropertyDescriptor Implements ICustomTypeDescriptor.GetDefaultProperty
        Return Nothing
    End Function

    Private Function GetEditor(editorBaseType As Type) As Object Implements ICustomTypeDescriptor.GetEditor
        Return Nothing
    End Function

    Private Function GetEvents() As EventDescriptorCollection Implements ICustomTypeDescriptor.GetEvents
        Return New EventDescriptorCollection(Nothing)
    End Function

    Private Function GetEvents(attributes As Attribute()) As EventDescriptorCollection Implements ICustomTypeDescriptor.GetEvents
        Return New EventDescriptorCollection(Nothing)
    End Function

    Private Function GetProperties() As PropertyDescriptorCollection Implements ICustomTypeDescriptor.GetProperties
        Return DirectCast(Me, ICustomTypeDescriptor).GetProperties(Nothing)
    End Function

#End Region

End Class

''' <summary>
''' Custom PropertyDescriptor used by the EntitySetTypeDescriptor (above)
''' to expose EntitySets as properties.
''' </summary>
''' <remarks>
''' Reflected from DataTablePropertyDescriptor
''' </remarks>
Public Class EntitySetPropertyDescriptor
    Inherits PropertyDescriptor

#Region "Fields"

    Private m_View As EntitySet

#End Region

#Region "Constructors"

    Friend Sub New(ByVal view As EntitySet)
        MyBase.New(view.Name, Nothing)
        m_View = view
    End Sub

#End Region

#Region "Overrides"

    Public Overrides Function CanResetValue(ByVal component As Object) As Boolean
        Return False
    End Function

    Public Overrides Function Equals(ByVal other As Object) As Boolean
        If TypeOf other Is EntitySetPropertyDescriptor Then
            Dim descriptor As EntitySetPropertyDescriptor = DirectCast(other, EntitySetPropertyDescriptor)
            Return (descriptor.m_View Is m_View)
        End If
        Return False
    End Function

    Public Overrides Function GetHashCode() As Integer
        Return m_View.GetHashCode()
    End Function

    Public Overrides Function GetValue(ByVal component As Object) As Object
        Return m_View.List
    End Function

    Public Overrides Sub ResetValue(ByVal component As Object)
    End Sub

    Public Overrides Sub SetValue(ByVal component As Object, value As Object)
    End Sub

    Public Overrides Function ShouldSerializeValue(ByVal component As Object) As Boolean
        Return False
    End Function

    Public Overrides ReadOnly Property ComponentType() As Type
        Get
            Return GetType(EntitySet)
        End Get
    End Property

    Public Overrides ReadOnly Property IsReadOnly() As Boolean
        Get
            Return False
        End Get
    End Property

    Public Overrides ReadOnly Property PropertyType() As Type
        Get
            Return GetType(IBindingList)
        End Get
    End Property

#End Region

End Class